// 1.首先定义全部路由
// 2.获取用户权限
// 3.根据用户权限过滤出权限路由
// (定义一个空的路由表，过滤出权限过后使用vue4中的addroute()进行动态路由添加)
import type { RouteRecordRaw } from "vue-router";
import router from "../router/index";

type IRoute = RouteRecordRaw & { id?: string };
// 定义全部路由表，并给路由添加权限 HomeIndex的下一级路由
const curRoute: IRoute[] = [
    // 重定向到主页
    {
        id: "1",
        name: "/",
        path: "",
        redirect: "/HomeIndex/HomePage"
    },
    //注意：动态添加路由的时候，子路由的path前面不用加/
    // 1.首页
    {
        id: "1",
        name: "HomePage",
        path: "HomePage",
        component: () => import("../views/Home/HomeIndex.vue")
    },
    // 2.商品管理-商品列表,分类搜索
    {
        id: "21",
        name: "GoodsList",
        path: "GoodsList",
        component: () => import("../views/Home/Goods/List.vue")
    },
    {
        id: "22",
        name: "GoodsCategory",
        path: "GoodsCategory",
        component: () => import("../views/Home/Goods/Category.vue")
    },
    // 3.轮播图管理-轮播图列表,添加轮播图
    {
        id: "31",
        name: "BanList",
        path: "BanList",
        component: () => import("../views/Home/Banner/List.vue")
    },
    {
        id: "32",
        name: "BanAdd",
        path: "BanAdd",
        component: () => import("../views/Home/Banner/Add.vue")
    },
    // 4.操作人员管理-操作人员列表,添加操作人员
    {
        id: "41",
        name: "AdminList",
        path: "AdminList",
        component: () => import("../views/Home/Admin/List.vue"),
    },
    {
        id: "42",
        name: "AdminAdd",
        path: "AdminAdd",
        component: () => import("../views/Home/Admin/Add.vue")
    },
    // 5.购物车管理-购物车列表
    {
        id: "51",
        name: "ShoppingList",
        path: "ShoppingList",
        component: () => import("../views/Home/ShoppingCar/ShoppingList.vue")
    },
    // 6.用户信息-用户列表
    {
        id: "61",
        name: "UserList",
        path: "UserList",
        component: () => import("../views/Home/User/UserList.vue")
    },
    // 7.订单信息-订单列表
    {
        id: "71",
        name: "OrderList",
        path: "OrderList",
        component: () => import("../views/Home/Order/OrderList.vue")
    },
    // 8.数据图表-数据图表,简单图表数据
    {
        id: "81",
        name: "ChartKLine",
        path: "ChartKLine",
        component: () => import("../views/Home/Chart/KLine.vue")
    },
    {
        id: "82",
        name: "ChartData",
        path: "ChartData",
        component: () => import("../views/Home/Chart/Chartdata.vue")
    }
]
export default function getRoutes() {
    // 创建一个空数组用来接收过滤后的权限
    let checkedKeys: string[] = [];
    // 获取当前用户的权限
    const currentgrant = sessionStorage.getItem("checkedkeys");
    // 判断权限是否存在
    if (currentgrant) {
        // 存在的话存储在创建的空数组中
        checkedKeys = currentgrant.split(',');
    }
    // 如果当前用户的权限为空,则给当前用户添加所有权限
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '11', '2', '21', '22', '3', '31', '31-C', '31-D', '32', '4', '41', '41-C', '41-D', '42', '5', '51', '6', '61', '7', '71', '71-C', '71-D', '8', '81', '82'];
    }
    // 2、获取当前用户的路由配置
    // 遍历所有权限路由,拿存储在定义的空数组中的用户权限路由进行判断、过滤
    curRoute.forEach((item) => {
        if (checkedKeys.includes(item.id as string)) {
            // 定义一个变量用来接收每一个item,目的是为了给每一个item添加一个crud
            let currRoute = { ...item }
            let arr = ["0", "0", "0", "0"];
            if (checkedKeys.includes(item.id + "-C")) {
                arr[0] = "1";
            }
            if (checkedKeys.includes(item.id + "-R")) {
                arr[1] = "1";
            }
            if (checkedKeys.includes(item.id + "-U")) {
                arr[2] = "1";
            }
            if (checkedKeys.includes(item.id + "-D")) {
                arr[3] = "1";
            }
            currRoute.meta = {
                crud: arr.join("")
            }
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            router.addRoute("HomeIndex", currRoute);
        }
    })
    // console.log("router.getRoutes()", router.getRoutes());
}

// 清除当前用户的路由，这是当用户点击退出时执行的操作
export const clearRoutes = () => {
    // 1、获取当前用户的权限
    let checkedKeys: string[] = [];
    const grants = sessionStorage.getItem("grants");
    if (grants) {
        checkedKeys = grants.split(',');
    }
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '11', '2', '21', '22', '3', '31', '32', '4', '41', '42', '5', '51', '6', '61', '7', '71', '8', '81', '82'];
    }

    // 2、删除当前用户的路由配置

    curRoute.forEach(item => {
        if (checkedKeys.includes(item.id as string)) {
            // 动态添加路由配置： vueRouter对象.addRoute(父级路由的name，路由配置);
            //    router.addRoute("Admin",{...item});
            router.removeRoute(item.name as string);
        }
    })
}